<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla security
defined('_JEXEC') or die;
jimport('joomla.application.component.controlleradmin');

// Controller Samsong view
class JooSamControllerSamsong extends JControllerAdmin{
	protected $text_prefix = 'Samsong';

	public function cancel(){
		$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=samsongs', false));
	}

	public function save(){
		$send = 0;
		$error = 0;
		$data_server = JRequest::getVar('server_id', 'jsmerror');
		$data_request = JRequest::getVar('request_song_id', 'jsmerror');
		$data_request_ip = JRequest::getVar('request_song_ip', 'jsmerror');
		$data_request_name = JRequest::getVar('request_song_name', 'jsmerror');
		$data_request_name = str_replace('"', "'", $data_request_name);
		$data_request_msg = JRequest::getVar('request_song_msg', 'jsmerror');
		$data_request_msg = str_replace('"', "'", $data_request_msg);
		require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_joosam' . DS . 'helpers' . DS . 'helper.samdb.php';
		require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_joosam' . DS . 'helpers' . DS . 'helper.request.php';
		$output = '<table>';
		$savedata = SamdbHelper::samdb('updateSongDate', $data_server);
		$resp1 = $savedata['status'];
		if($resp1 == 'valid'){
			if($data_request != 'jsmerror' AND $data_request != ''){
				$request = RequestHelper::request($data_server, $data_request_ip, $data_request);
				$req_status = $request['status'];
				$req_txt = (int)$request['txtcode'];
				$req_id = $request['rid'];
				if($req_txt == 200 and $data_request_name != 'jsmerror' and $data_request_name != ''){
					$send_msg = SamdbHelper::samdb('updateRequestData', $data_server, $req_id);
					$resp2 = $send_msg['status'];
					if($resp2 == 'valid'){ $send = 1; }
				}
			}
			$output .= '<tr><td>'.$savedata['data'].'</td></tr>';
			if($send == 1){
				$textwork = $this->getTxTcode($data_server, $req_txt);
				if($textwork['status'] == 'valid'){ $output .= '<tr><td>'.$textwork['data'].'</td></tr>'; }
				else{$error = 1; $output .= '<tr><td>'.JText::_('COM_JOOSAM_SAMSONG_CONT_NO_RESPONSE_STRING').$textwork['data'].'</td></tr>'; }
			}
		}
		else{$error = 1; $output .= '<tr><td>'.$savedata['data'].'</td></tr>'; }
		$output .= '</table>';
		// Output
		if($error == 1){ $this->setMessage($output, 'warning'); }
		else{ $this->setMessage($output); }
		$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=samsongs', false));
	}

	protected function getTxTcode($srv_id, $txt_code){
		
		$db =& JFactory::getDBO();
		$query = "
			SELECT ".$db->nameQuote('txt_string')."
			FROM ".$db->nameQuote('#__joosam_textstrings')."
			WHERE ".$db->nameQuote('srv_id')." = ".$db->quote($srv_id)."
			AND ".$db->nameQuote('txt_code')." = ".$db->quote($txt_code)." ; ";
		$db->setQuery($query);
		$txt = $db->loadResult();
		if($txt){
			$result = array('status' => 'valid', 'data' => $txt);
		}
		else{
			$result = array('status' => 'error', 'data' => $txt_code);
		}
		return $result;
	}
}